package com.ef.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ELeadsDealType;
import com.ef.constant.ETimeline;
import com.ef.domain.ClassEntity;
import com.ef.domain.ClassFocus;
import com.ef.domain.ClassLevel;
import com.ef.domain.ClassType;
import com.ef.domain.Leads;
import com.ef.domain.LeadsClassEntity;
import com.ef.domain.LeadsDealHistory;
import com.ef.domain.Teacher;
import com.ef.service.ClassArrangeService;
import com.ef.service.ClassBookingService;
import com.ef.service.ClassFocusService;
import com.ef.service.ClassLevelService;
import com.ef.service.ClassTypeService;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsDealHistoryService;
import com.ef.service.LeadsService;
import com.ef.service.TeacherService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;
import com.ef.util.LeadsClassEntityExample;

/**
 * 课程检查 .<br/>
 * 
 * @author CYG create:2009-12-12 上午09:47:20
 */
public class ClassCheckAction extends BaseAction<LeadsClassEntity> {

	private static final long serialVersionUID = 6471086700301676748L;

	private LeadsClassEntityService leadsClassEntityService;

	private ClassBookingService classBookingService;

	private ClassArrangeService classArrangeService;

	private TeacherService teacherService;

	private LeadsService leadsService;

	private LeadsDealHistoryService leadsDealHistoryService;

	private ClassTypeService classTypeService;

	private ClassLevelService classLevelService;

	private ClassFocusService classFocusService;

	private Map<String, String> checkMessageMap = new LinkedHashMap<String, String>();

	private List<ClassType> classTypeList;

	private List<ClassFocus> classFocusList;

	private List<ClassLevel> classLevelList;

	private Date selDate;

	private String selTime;
	/**
	 * classEntity
	 */
	private Long selClassEntityId;

	// 以下是时间间隔的情况
	private Date selBookingStartTime;

	private Date selBookingEndTime;

	private Date selCheckinStartTime;

	private Date selCheckinEndTime;

	private Date selCancelStartTime;

	private Date selCancelEndTime;

	private Date selClassStartTime;

	private Date selClassEndTime;

	private Teacher classEntityTeacher;

	private ClassType classEntityClassType;

	private ClassLevel classEntityClassLevel;

	private ClassFocus classEntityClassFocus;

	private ETimeline classEntityClassTime;

	private Date punishStartTime;

	private Date punishEndTime;

	private Integer year;

	private Integer weekStart;

	private Integer weekEnd;

	public ClassCheckAction() {
		selDate = new Date();
		int pyear = CalendarUtil.getYear();
		int pweek = CalendarUtil.getWeekOfYearWithMondayIsFirstDay(new Date());
		punishStartTime = CalendarUtil.getMondayOfWeekWithMonDayIsFirstDay(
				pyear, pweek + 1);
		punishEndTime = CalendarUtil.getSundayOfWeekWithMonDayIsFirstDay(pyear,
				pweek + 1);
		Calendar now = Calendar.getInstance();
		now.setFirstDayOfWeek(Calendar.MONDAY);
		int day = now.get(Calendar.DAY_OF_YEAR);
		weekStart = now.get(Calendar.WEEK_OF_YEAR);
		weekEnd = weekStart;
		year = now.get(Calendar.YEAR);
		if (day > 7 && weekStart == 1) {
			year++;
		}
		// //NOTE week要-1
		if (year == 2010) {
			weekStart = weekStart - 1;
			weekEnd = weekEnd - 1;
		}
	}

	public Boolean isCR(){
		if (getSessionUser().getRole()!=null){
			if (getSessionUser().getRole().getName().trim().toUpperCase().startsWith("CR")){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	/**
	 * 在时间段里面的所有课程
	 */
	private List<ClassEntity> allClassEntity;

	@SuppressWarnings("serial")
	public String list() {
		fetchListData2();
		CustomExample<LeadsClassEntity> example = new LeadsClassEntityExample(
				this.getModel(), LeadsClassEntity.class) {
			public void appendCondition(Criteria criteria) {
				super.appendCondition(criteria);
				int trueWeekStart = weekStart;
				int trueWeekEnd = weekEnd;
				if (year == 2010) {
					trueWeekStart = trueWeekStart + 1;
					trueWeekEnd = trueWeekEnd + 1;
				}
				Criteria cc = criteria.createCriteria("classEntity",
						"classEntity");
				if (selClassStartTime == null && selClassEndTime == null) {
					// year ,week 查询
					cc.add(
							Restrictions.ge("classDate", CalendarUtil
									.getMondayOfWeekWithMonDayIsFirstDay(year,
											trueWeekStart))).add(
							Restrictions.le("classDate", CalendarUtil
									.getSundayOfWeekWithMonDayIsFirstDay(year,
											trueWeekEnd)));
				}
				if (getClassEntityClassFocus() != null
						&& getClassEntityClassFocus().getId() != null) {
					cc.createCriteria("classFocus").add(
							Restrictions.eq("id", getClassEntityClassFocus()
									.getId()));
				}
				if (getClassEntityClassType() != null
						&& getClassEntityClassType().getId() != null) {
					cc.createCriteria("classType").add(
							Restrictions.eq("id", getClassEntityClassType()
									.getId()));
				}
				if (getClassEntityClassLevel() != null
						&& getClassEntityClassLevel().getId() != null) {
					cc.createCriteria("classLevel").add(
							Restrictions.eq("id", getClassEntityClassLevel()
									.getId()));
				}
				if (getClassEntityTeacher() != null
						&& getClassEntityTeacher().getId() != null) {
					cc.createCriteria("teacher").add(
							Restrictions.eq("id", getClassEntityTeacher()
									.getId()));
				}
				if (classEntityClassTime != null) {
					cc.add(Restrictions.eq("classTime", classEntityClassTime));
					// System.out.println(getClassEntityClassTime());
				}
				criteria.addOrder(Order.desc("classEntity.classDate"))
						.addOrder(Order.asc("classEntity.classTime")).addOrder(
								Order.asc("classEntity.id")).addOrder(
								Order.desc("bookingTime"));
			}
		};
		example.addBetweenDateProperty("bookingTime", selBookingStartTime,
				selBookingEndTime);
		example.addBetweenDateProperty("checkinTime", selCheckinStartTime,
				selCheckinEndTime);
		example.addBetweenDateProperty("cancelTime", selCancelStartTime,
				selCancelEndTime);
		example.addBetweenDateProperty("classEntity.classDate",
				selClassStartTime, selClassEndTime);
		this.listResult = leadsClassEntityService.findByExample(example, this
				.getStartIndex(), this.getPageSize());
		return LIST;
	}

	protected void fetchListData() {
		// System.out.println(this.selTime);
		if (selTime != null && selTime.trim().length() > 0) {
			allClassEntity = classArrangeService.getClassEntityByDateTime(
					selDate, ETimeline.valueOf(selTime.trim()));
		} else {
			allClassEntity = new ArrayList<ClassEntity>();
		}
		List<ETimeline> times = null;
		if (CalendarUtil.isWeekend(selDate)) {
			times = ETimeline.getWeekendTimeline();
		} else {
			times = ETimeline.getNomarlTimeline();
		}
		for (ETimeline timeline : times) {
			String msg = classArrangeService.getClassCheckMessage(selDate,
					timeline);
			checkMessageMap.put(timeline.name(), timeline.toString() + "    "
					+ msg);
		}
		classTypeList = classTypeService.findALLValid();
		classLevelList = classLevelService.findALLValid();
		classFocusList = classFocusService.findALLValid();
	}

	protected void fetchListData2() {
		// System.out.println(this.selTime);
		allClassEntity = new ArrayList<ClassEntity>();
		List<ETimeline> times = null;
		if (CalendarUtil.isWeekend(selDate)) {
			times = ETimeline.getWeekendTimeline();
		} else {
			times = ETimeline.getNomarlTimeline();
		}
		for (ETimeline timeline : times) {
			String msg = classArrangeService.getClassCheckMessage(selDate,
					timeline);
			checkMessageMap.put(timeline.name(), timeline.toString() + "    "
					+ msg);
		}
		classTypeList = classTypeService.findALLValid();
		classLevelList = classLevelService.findALLValid();
		classFocusList = classFocusService.findALLValid();
	}

	public String doPunish() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			LeadsClassEntity leadsClassEntity = leadsClassEntityService
					.findById(Long.valueOf(i.trim()));
			Leads leads = leadsClassEntity.getLeads();
			leads.setPunishStartTime(punishStartTime);
			leads.setPunishEndTime(punishEndTime);
			leads.setNeedPunish(true);
			leadsService.saveOrUpdate(leads);
			LeadsDealHistory dh = new LeadsDealHistory();
			dh.setCurState("Punish");
			dh.setDealTime(new Date());
			dh.setOperUser(this.getSessionUser());
			dh.setType(ELeadsDealType.Punish);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			dh.setLeads(leads);
			if (punishStartTime != null && punishEndTime != null) {
				dh.setComments("Punish From " + df.format(punishStartTime)
						+ " to " + df.format(punishEndTime));
			}
			leadsDealHistoryService.saveOrUpdate(dh);

		}
		return list();
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public void setClassBookingService(ClassBookingService classBookingService) {
		this.classBookingService = classBookingService;
	}

	public ClassBookingService getClassBookingService() {
		return classBookingService;
	}

	public LeadsService getLeadsService() {
		return leadsService;
	}

	public void setLeadsService(LeadsService leadsService) {
		this.leadsService = leadsService;
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	public Map<String, String> getCheckMessageMap() {
		return checkMessageMap;
	}

	public void setCheckMessageMap(Map<String, String> checkMessageMap) {
		this.checkMessageMap = checkMessageMap;
	}

	public ClassArrangeService getClassArrangeService() {
		return classArrangeService;
	}

	public void setClassArrangeService(ClassArrangeService classArrangeService) {
		this.classArrangeService = classArrangeService;
	}

	public Date getSelDate() {
		return selDate;
	}

	public void setSelDate(Date selDate) {
		this.selDate = selDate;
	}

	public Long getSelClassEntityId() {
		return selClassEntityId;
	}

	public void setSelClassEntityId(Long selClassEntityId) {
		this.selClassEntityId = selClassEntityId;
	}

	public String getSelTime() {
		return selTime;
	}

	public void setSelTime(String selTime) {
		this.selTime = selTime;
	}

	public List<ClassEntity> getAllClassEntity() {
		return allClassEntity;
	}

	public void setAllClassEntity(List<ClassEntity> allClassEntity) {
		this.allClassEntity = allClassEntity;
	}

	public Date getSelBookingStartTime() {
		return selBookingStartTime;
	}

	public void setSelBookingStartTime(Date selBookingStartTime) {
		this.selBookingStartTime = selBookingStartTime;
	}

	public Date getSelBookingEndTime() {
		return selBookingEndTime;
	}

	public void setSelBookingEndTime(Date selBookingEndTime) {
		this.selBookingEndTime = selBookingEndTime;
	}

	public Date getSelCheckinStartTime() {
		return selCheckinStartTime;
	}

	public void setSelCheckinStartTime(Date selCheckinStartTime) {
		this.selCheckinStartTime = selCheckinStartTime;
	}

	public Date getSelCheckinEndTime() {
		return selCheckinEndTime;
	}

	public void setSelCheckinEndTime(Date selCheckinEndTime) {
		this.selCheckinEndTime = selCheckinEndTime;
	}

	public Date getSelCancelStartTime() {
		return selCancelStartTime;
	}

	public void setSelCancelStartTime(Date selCancelStartTime) {
		this.selCancelStartTime = selCancelStartTime;
	}

	public Date getSelCancelEndTime() {
		return selCancelEndTime;
	}

	public void setSelCancelEndTime(Date selCancelEndTime) {
		this.selCancelEndTime = selCancelEndTime;
	}

	public Date getSelClassStartTime() {
		return selClassStartTime;
	}

	public void setSelClassStartTime(Date selClassStartTime) {
		this.selClassStartTime = selClassStartTime;
	}

	public Date getSelClassEndTime() {
		return selClassEndTime;
	}

	public void setSelClassEndTime(Date selClassEndTime) {
		this.selClassEndTime = selClassEndTime;
	}

	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}

	public ClassLevelService getClassLevelService() {
		return classLevelService;
	}

	public void setClassLevelService(ClassLevelService classLevelService) {
		this.classLevelService = classLevelService;
	}

	public ClassFocusService getClassFocusService() {
		return classFocusService;
	}

	public void setClassFocusService(ClassFocusService classFocusService) {
		this.classFocusService = classFocusService;
	}

	public List<ClassType> getClassTypeList() {
		return classTypeList;
	}

	public void setClassTypeList(List<ClassType> classTypeList) {
		this.classTypeList = classTypeList;
	}

	public List<ClassFocus> getClassFocusList() {
		return classFocusList;
	}

	public void setClassFocusList(List<ClassFocus> classFocusList) {
		this.classFocusList = classFocusList;
	}

	public List<ClassLevel> getClassLevelList() {
		return classLevelList;
	}

	public void setClassLevelList(List<ClassLevel> classLevelList) {
		this.classLevelList = classLevelList;
	}

	public Teacher getClassEntityTeacher() {
		return classEntityTeacher;
	}

	public void setClassEntityTeacher(Teacher classEntityTeacher) {
		this.classEntityTeacher = classEntityTeacher;
	}

	public ClassType getClassEntityClassType() {
		return classEntityClassType;
	}

	public void setClassEntityClassType(ClassType classEntityClassType) {
		this.classEntityClassType = classEntityClassType;
	}

	public ClassLevel getClassEntityClassLevel() {
		return classEntityClassLevel;
	}

	public void setClassEntityClassLevel(ClassLevel classEntityClassLevel) {
		this.classEntityClassLevel = classEntityClassLevel;
	}

	public ClassFocus getClassEntityClassFocus() {
		return classEntityClassFocus;
	}

	public void setClassEntityClassFocus(ClassFocus classEntityClassFocus) {
		this.classEntityClassFocus = classEntityClassFocus;
	}

	public String getClassEntityClassTime() {
		if (classEntityClassTime!=null){
		    return classEntityClassTime.name();
		}else {
			return "";
		}
	}

	public void setClassEntityClassTime(String classEntityClassTime) {
		this.classEntityClassTime = ETimeline.valueOf(classEntityClassTime);
	}

	public Date getPunishStartTime() {
		return punishStartTime;
	}

	public void setPunishStartTime(Date punishStartTime) {
		this.punishStartTime = punishStartTime;
	}

	public Date getPunishEndTime() {
		return punishEndTime;
	}

	public void setPunishEndTime(Date punishEndTime) {
		this.punishEndTime = punishEndTime;
	}

	public LeadsDealHistoryService getLeadsDealHistoryService() {
		return leadsDealHistoryService;
	}

	public void setLeadsDealHistoryService(
			LeadsDealHistoryService leadsDealHistoryService) {
		this.leadsDealHistoryService = leadsDealHistoryService;
	}

	public void setClassEntityClassTime(ETimeline classEntityClassTime) {
		this.classEntityClassTime = classEntityClassTime;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getWeekStart() {
		return weekStart;
	}

	public void setWeekStart(Integer weekStart) {
		this.weekStart = weekStart;
	}

	public Integer getWeekEnd() {
		return weekEnd;
	}

	public void setWeekEnd(Integer weekEnd) {
		this.weekEnd = weekEnd;
	}

}
